/******************************************************************************
* File : AnalogClock.java
* Author : http://java.macteki.com/
* Description :
*   Display an analog clock.
* Tested with : JDK 1.6
******************************************************************************/

import javax.swing.JLabel;

class AnalogClock extends javax.swing.JPanel
{
  public static AnalogClock getInstance()
  {
    AnalogClock panel=new AnalogClock();
    panel.setPreferredSize(new java.awt.Dimension(300,300));
    return panel;
  }

  public void paintComponent(java.awt.Graphics graphics)
  {
    super.paintComponent(graphics);
 
    java.awt.Graphics2D gr=(java.awt.Graphics2D) graphics;    

    gr.drawArc(0,0,200,200,0,360);

    int xCenter=100,yCenter=100;

    java.util.Calendar calendar=java.util.Calendar.getInstance();
    int second=calendar.get(java.util.Calendar.SECOND);

    // polar coordinate of the 'second' arm
    // This arm start at 90 degree and it should move 6 degree per second
    double degree = 90-second*6;
    int radius = 80;
    double pi=3.141592654;

    int x=(int) (radius*Math.cos(degree*pi/180));
    int y=(int) (radius*Math.sin(degree*pi/180));

    y=-y;  // positive y-axis should point upwards.

    // translate the whole coordinate system.
    x+=xCenter;
    y+=yCenter;

    gr.drawLine(xCenter,yCenter,x,y);


    // draw the minute arm, it should move 0.1 degree every second.
    int minute=calendar.get(java.util.Calendar.MINUTE);
    double secondPassedInThisHour = minute*60+second;
    degree = 90.0 - secondPassedInThisHour*(0.1);
    radius = 80;

    x=(int) (radius*Math.cos(degree*pi/180));
    y=(int) (radius*Math.sin(degree*pi/180));

    y=-y;  // positive y-axis should point upwards.

    // translate the whole coordinate system.
    x+=xCenter;
    y+=yCenter;
    
    gr.drawLine(xCenter,yCenter,x,y);    


    // draw the hour arm, since this arm should move 360 degree in 12 hour
    // 12 hour = 43200 seconds
    // it should move 360/43200 degree every second.
    int hour=calendar.get(java.util.Calendar.HOUR);
    if (hour>12) hour-=12;
    double secondPassed = hour*3600+minute*60+second;
    degree = 90.0 - secondPassed*(360.0/43200.0);
    radius = 60;  // make it shorter

    x=(int) (radius*Math.cos(degree*pi/180));
    y=(int) (radius*Math.sin(degree*pi/180));

    y=-y;  // positive y-axis should point upwards.

    // translate the whole coordinate system.
    x+=xCenter;
    y+=yCenter;
    
    gr.drawLine(xCenter,yCenter,x,y);    

  }

  public static void main(String[] args) throws Exception
  {
    // create frame
    javax.swing.JFrame frame=new javax.swing.JFrame();
    frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);    

    frame.setTitle("Macteki AnalogClock");

    AnalogClock clockPanel=AnalogClock.getInstance();

    // create a image label
    JLabel digitalLabel=new JLabel("", JLabel.CENTER); 
    digitalLabel.setBounds(0,250,300,30);
    clockPanel.setLayout(null);
    clockPanel.add(digitalLabel);

    frame.add(clockPanel);  
    frame.pack();  // set the window size big enough to hold its component
    frame.setVisible(true); 

    while (true)
    {
      java.util.Date date=new java.util.Date();
      digitalLabel.setText(""+date);
      clockPanel.repaint();
      Thread.sleep(500);
    }

  }
}